﻿using org.xisoft.game.utils.game;
using org.xisoft.game.utils.game.algorithms;
using ReversiModule.Chess.Algorithms;
using System;
using System.Collections.Generic;
using System.IO;

namespace ReversiModule.Chess
{
    [Serializable]
    class ReversiBoard : ChessBoardBase
    {
        public const int MAXX = 10;
        public const int MAXY = 10;




        private byte itemCount;


        public ReversiBoard()
            :base(MAXX, MAXY)
        {

        }

  


        public override void Undo()
        {
            base.Undo();
            itemCount--;

        }

        public override void DoNextMove(MoveAction moveAction)
        {
            itemCount++;

            int x = moveAction.ToX;
            int y = moveAction.ToY;

            int score = 0;
            int tmpScore = 0;

            for (int dx = -1; dx < 2; dx++)
                for (int dy = -1; dy < 2; dy++)
                {
                    if (dx == 0 && dy == 0) continue;

                    tmpScore = GetLinkCount(this,x, y,dx,dy);
                    score += tmpScore;
                    int left = x +dx;
                    int top = y+dy;
                    while (tmpScore > 0)
                    {
                        this[left, top].IsBlack = IsCurrentBlackMove;
                        left += dx;
                        top += dy;
                        tmpScore--;
                    }
                }


            ChessItem chessItem= new ChessItem { IsBlack = IsCurrentBlackMove, Index = itemCount++, PositionX = x, PositionY = y };
            moveAction. CurrentChessman = chessItem;

            base.DoNextMove(moveAction);

        }



        public override void Reset()
        {
            base.Reset();
            int halfX =(MaxX - 1) / 2;
            int halfY =(MaxY - 1) / 2;
            this[halfX, halfY] = new ChessItem { IsBlack = false, Index = itemCount++, PositionX=halfX, PositionY=halfY };
            this[halfX, halfY + 1] = new ChessItem { IsBlack = true, Index = itemCount++, PositionX = halfX, PositionY =halfY +1};
            this[halfX + 1, halfY + 1] = new ChessItem { IsBlack = false, Index = itemCount++, PositionX = halfX +1, PositionY = halfY +1};
            this[halfX + 1, halfY] = new ChessItem { IsBlack = true, Index = itemCount++, PositionX = halfX +1, PositionY = halfY };

        }

        protected override void Undo(MoveAction moveAction)
        {
            
        }

        public int GetLinkCount(ChessBoardBase board, int x, int y, int dx, int dy)
        {
            int top = y + dy;
            int left = x + dx;
            int count = 0;
            while (top >= 0 && top < board.MaxY && left >= 0 && left < board.MaxX)
            {
                ChessItem item = board[left, top];
                if (item == null)
                    return 0;
                if (item.IsBlack == board.IsCurrentBlackMove)
                    return count;
                top += dy;
                left += dx;
                count++;
            }
            return 0;
        }
    }
}
